package org.weasel.business;

import java.sql.Time;
import java.util.ArrayList;

import org.weasel.data.ProjectManager;

public class Iteration extends Item {
	private String id;
	private String name;
	private ArrayList<UserStory> userStories;
	private ProjectManager manager;
	
	public Iteration(String id, String name, ArrayList<UserStory> userStories, ProjectManager manager) {
		this.id = id;
		this.name = name;
		if(userStories == null) {
			this.userStories = new ArrayList<UserStory>();
		} else {
			this.userStories = userStories;
		}
		this.manager = manager;
	}
	
	public UserStory createAndAddUserStory(String name, int businessValue, Time estimatedTime, Time usedTime, int effort) throws Exception {
		UserStory story = manager.createUserStory(this.id, name, businessValue, estimatedTime, usedTime, effort);
		userStories.add(story);
		return story;
	}
	
	public void addUserStory(UserStory userStory) throws Exception {
		UserStory story = manager.createUserStory(this.id, userStory.getName(), userStory.getBusinessValue(), userStory.getEstimatedTime(), userStory.getUsedTime(), userStory.getEffort());
		userStories.add(story);
	}
	
	public Task addTask(String storyID, String description){
		Task tsk=null;
		try {
			tsk = manager.createTask(storyID, description);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tsk;
		
	}
	
	public void removeTask(String taskID){
		try {
			manager.deleteTask(taskID);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public UserStory getUserStory(String userStoryID) {
		for (UserStory userStory : userStories) {
			if(userStory.getId().equals(userStoryID)) {
				return userStory;
			}
		}
		return null;
	}
	
	public void removeUserStory(String userStoryID) throws Exception {
		for (int i = 0; i < userStories.size(); i++) {
			if(userStories.get(i).getId().equals(userStoryID)) {
				//also delete it from the XML file
				manager.deleteUserStory(userStoryID);
				userStories.remove(i);
			}
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ArrayList<UserStory> getUserStories() {
		return userStories;
	}

	public void setUserStories(ArrayList<UserStory> userStories) {
		this.userStories = userStories;
	}

	public String getId() {
		return id;
	}
}
